package com.cwz.quartz.controller;


import com.cwz.api.entity.SysJobLog;
import com.cwz.core.result.R;
import com.cwz.core.utils.excel.ExcelUtils;
import com.cwz.log.annotation.Log;
import com.cwz.log.enums.BusinessType;
import com.cwz.quartz.model.SysJobLogVo;
import com.cwz.quartz.service.SysJobLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 定时任务调度日志表 前端控制器
 * </p>
 *
 * @author Wen
 * @since 2023-08-24
 */
@RestController
@RequestMapping("/job/log")
public class SysJobLogController {
	@Autowired
	private SysJobLogService sysJobLogService;

	/**
	 * 查询定时任务调度日志列表
	 */
	@GetMapping("/list")
	public R<Map<String, Object>> list(SysJobLogVo sysJobLog) {
		return R.ok(this.sysJobLogService.selectJobLogList(sysJobLog));
	}

	/**
	 * 根据调度编号获取详细信息
	 */
	@GetMapping(value = "/{jobLogId}")
	public R<SysJobLog> getInfo(@PathVariable Long jobLogId) {
		return R.ok(this.sysJobLogService.selectJobLogById(jobLogId));
	}

	/**
	 * 删除定时任务调度日志
	 */
	@Log(title = "定时任务调度日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{jobLogIds}")
	public R<Integer> remove(@PathVariable Long[] jobLogIds) {
		return R.ok(this.sysJobLogService.deleteJobLogByIds(jobLogIds));
	}

	/**
	 * 清空定时任务调度日志
	 */
	@Log(title = "调度日志", businessType = BusinessType.CLEAN)
	@DeleteMapping("/clean")
	public R<Void> clean() {
		this.sysJobLogService.cleanJobLog();
		return R.ok("清空任务日志成功");
	}

	/**
	 * 导出定时任务调度日志列表
	 */
	@Log(title = "任务调度日志", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, SysJobLogVo sysJobLog) {
		List<SysJobLog> list = (List<SysJobLog>) this.sysJobLogService.selectJobLogList(sysJobLog).get("list");
		ExcelUtils<SysJobLog> utils = new ExcelUtils<>(SysJobLog.class);
		utils.exportExcel(response, list, "调度日志");
	}
}

